package com.sairobo.base.modules.sys.controller;

import com.google.gson.Gson;
import com.sairobo.base.common.annotation.SysLog;
import com.sairobo.base.common.constant.ConfigConstant;
import com.sairobo.base.common.constant.Constant;
import com.sairobo.base.common.entity.Page;
import com.sairobo.base.common.entity.R;
import com.sairobo.base.common.exception.RRException;
import com.sairobo.base.common.validator.ValidatorUtils;
import com.sairobo.base.common.validator.group.AliyunGroup;
import com.sairobo.base.common.validator.group.QcloudGroup;
import com.sairobo.base.common.validator.group.QiniuGroup;
import com.sairobo.base.modules.cloud.CloudStorageConfig;
import com.sairobo.base.modules.cloud.OSSFactory;
import com.sairobo.base.modules.sys.entity.SysOss;
import com.sairobo.base.modules.sys.service.SysConfigService;
import com.sairobo.base.modules.sys.service.SysOssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.Map;

/**
 * 文件上传
 *
 * @author admin
 * @email sairobo
 * @url www.sairobo.com
 * @date 2018年1月17日 下午7:58:51
 */
@RestController
@RequestMapping("/sys/oss")
public class SysOssController extends AbstractController {
	
	@Autowired
	private SysOssService sysOssService;

    @Autowired
    private SysConfigService sysConfigService;

    private final static String KEY = ConfigConstant.CLOUD_STORAGE_CONFIG_KEY;
	
	/**
	 * 列表
	 * @param params
	 * @return
	 */
	@RequestMapping("/list")
	public Page<SysOss> list(@RequestBody Map<String, Object> params) {
		return sysOssService.listSysOss(params);
	}
		
	/**
	 * 新增
	 * @param sysOss
	 * @return
	 */
	@SysLog("新增文件上传")
	@RequestMapping("/save")
	public R save(@RequestBody SysOss sysOss) {
		return sysOssService.saveSysOss(sysOss);
	}
	
	/**
	 * 根据id查询详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/info")
	public R getById(@RequestBody Long id) {
		return sysOssService.getSysOssById(id);
	}
	
	/**
	 * 修改
	 * @param sysOss
	 * @return
	 */
	@SysLog("修改文件上传")
	@RequestMapping("/update")
	public R update(@RequestBody SysOss sysOss) {
		return sysOssService.updateSysOss(sysOss);
	}



    /**
     * 云存储配置信息
     */
    @SysLog("云存储配置信息")
    @RequestMapping("/config")
    public R config(){
        CloudStorageConfig config = sysConfigService.getConfigObject(KEY, CloudStorageConfig.class);
        return R.ok().put("config", config);
    }

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@SysLog("删除文件上传")
	@RequestMapping("/remove")
	public R batchRemove(@RequestBody Long[] id) {
		return sysOssService.batchRemove(id);
	}


    /**
     * 上传文件
     */
    @RequestMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }

        //上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);

        //保存文件信息
        SysOss ossEntity = new SysOss();
        ossEntity.setUrl(url);
        ossEntity.setCreateDate(new Date());
        sysOssService.saveSysOss(ossEntity);

        return R.ok().put("url", url);
    }


    /**
     * 保存云存储配置信息
     */
    @RequestMapping("/saveConfig")
    public R saveConfig(@RequestBody CloudStorageConfig config){
        //校验类型
        ValidatorUtils.validateEntity(config);

        if(config.getType() == Constant.CloudService.QINIU.getValue()){
            //校验七牛数据
            ValidatorUtils.validateEntity(config, QiniuGroup.class);
        }else if(config.getType() == Constant.CloudService.ALIYUN.getValue()){
            //校验阿里云数据
            ValidatorUtils.validateEntity(config, AliyunGroup.class);
        }else if(config.getType() == Constant.CloudService.QCLOUD.getValue()){
            //校验腾讯云数据
            ValidatorUtils.validateEntity(config, QcloudGroup.class);
        }
        sysConfigService.updateValueByKey(KEY, new Gson().toJson(config));
        return R.ok();
    }

}
